package com.aerozhonghuan.logisticsmall.utils.permisson;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.content.ContextCompat;


/**
 * Created by zzw on 2018/3/29.
 */

public class PermissionCheckUtils {

    public static int checkPermissionCode = 1111;

    /**
     * 根据权限数组判断是否有授权
     * @param context
     * @param checkPermission
     * @return
     */
    public static boolean checkPerissonIntent(Activity context, String... checkPermission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PermissionCheckUtils.lacksPermissions(context, checkPermission)) {
                PermissionRequestUtils.startActivityForResult(context, checkPermissionCode,checkPermission);
                return false;
            } else {
                return true;
            }
        }
        return true;
    }

    /**
     * 根据权限数组判断是否有授权
     * @param context
     * @param checkPermission
     * @return
     */
    public static boolean checkPerisson(Activity context, String... checkPermission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PermissionCheckUtils.lacksPermissions(context, checkPermission)) {
                return false;
            } else {
                return true;
            }
        }
        return true;
    }
    /**
     *  判断权限集合
     * @param permissions
     * @return
     */
    public static boolean lacksPermissions(Context context,String... permissions) {
        for (String permission : permissions) {
            if (lacksPermission(context,permission)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否缺少权限
     * @param permission
     * @return true 没有权限
     *          false 有权限
     */
    public static boolean lacksPermission(Context context,String permission) {
        return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED;
    }

    /**
     * 获取手机厂商
     *
     * @return 手机厂商
     */
    public static String getDeviceBrand() {
        return Build.BRAND;
    }
}
